<?php

namespace BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class BannerController extends Controller
{
    //  编辑banner数据
    public function bannerOperaAction(Request $request)
    {
        if (!$this->get('session')->get('admin_name')) {
            return $this->render('@Backend/auth/login.html.twig');
        }
        $id = $request->get('id');

        $conn = $this->get('database_connection');
        $data['info'] = $conn->fetchAssoc("SELECT * FROM banner WHERE id = '". $id ."'");

        return $this->render('BackendBundle:banner:banner_opera.html.twig', $data);
    }

    // bannerlist
    public function bannerListAction(Request $request)
    {
        if (!$this->get('session')->get('admin_name')) {
            return $this->render('@Backend/auth/login.html.twig');
        }
        $name = $request->get('n');

        $em    = $this->get('doctrine.orm.entity_manager');
        if ($name === null) {
            $dql   = "SELECT a FROM BaseBundle:Banner a ORDER BY a.sortOrder ASC, a.hit DESC";
        } else {
            $dql   = "SELECT a FROM BaseBundle:Banner a WHERE a.title LIKE '%". $name ."%' OR a.hit LIKE '%". $name ."%' OR a.sortOrder LIKE '%". $name ."%' OR a.url LIKE '%". $name ."%'";
        }

        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            15 /*limit per page*/
        );

        return $this->render('BackendBundle:banner:banner_list.html.twig', array('pagination' => $pagination));
    }
}